﻿using JinJun.EF.Permission.Web.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JinJun.EF.Permission.Core;
using JinJun.EF.Permission.Model;
using JinJun.EF.Permission.BLL;

namespace JinJun.EF.Permission.Web.Attributes
{

    public class ActionAttribute:ActionFilterAttribute  //行为过滤器
    {

        /// <summary>
        /// 调用控制器对应的Action方法之后的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            SystemLogs systemLogs = new SystemLogs();
            string controller = filterContext.RouteData.Values["Controller"].ToString();
            string action = filterContext.RouteData.Values["Action"].ToString();           
            if (action == "LoginOut")
            {
                return;
            }
            if (action == "Login" || action == "SecurityCode")
            {
                //获取Ip
                string ip = LocalIP.GetLocalIP();                
                //获取值
                systemLogs.Controller = controller;
                systemLogs.Action= action;
                systemLogs.Time =DateTime.Now;
                systemLogs.UserName = ip;
                //创建业务层对象
                SystemLogsService systemLogsService = new SystemLogsService();
                //调用添加方法
                systemLogsService.Add(systemLogs);               
            }
            else
            {
                //获取用户名
                string username = AdminUserContext.adminContext.AdminUser.Name;
                //获取值
                systemLogs.Controller = controller;
                systemLogs.Action = action;
                systemLogs.Time = DateTime.Now;
                systemLogs.UserName = username;
                //创建业务层对象
                SystemLogsService systemLogsService = new SystemLogsService();
                //调用添加方法
                systemLogsService.Add(systemLogs);
            }
        }


        /// <summary>
        /// 调用控制器对应的Action方法之前的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            SystemLogs systemLogs = new SystemLogs();
            string controller = filterContext.RouteData.Values["Controller"].ToString();
            string action = filterContext.RouteData.Values["Action"].ToString();
            if (action == "Login"||action== "SecurityCode") 
            {
                //获取Ip
                string ip = LocalIP.GetLocalIP();
                //获取值
                systemLogs.Controller = controller;
                systemLogs.Action = action;
                systemLogs.Time = DateTime.Now;
                systemLogs.UserName = ip;           
                //创建业务层对象
                SystemLogsService systemLogsService = new SystemLogsService();
                //调用添加方法
                systemLogsService.Add(systemLogs);
            }
            else
            {
                //获取用户名
                string username = AdminUserContext.adminContext.AdminUser.Name;
                //获取值
                systemLogs.Controller = controller;
                systemLogs.Action = action;
                systemLogs.Time = DateTime.Now;
                systemLogs.UserName = username;
                //创建业务层对象
                SystemLogsService systemLogsService = new SystemLogsService();
                //调用添加方法
                systemLogsService.Add(systemLogs);
            }
        }



        /// <summary>
        /// 调用控制器对应的Action方法之后页面渲染之后的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {

        }

        /// <summary>
        /// 调用控制器对应的Action方法之后页面渲染之前的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {

        }
    }
}